/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.entity.ShippingMethod;
import com.hboxs.asl.service.DeliveryCorpService;
import com.hboxs.asl.service.ShippingMethodService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * Controller - 配送方式
 */
@Controller("adminShippingMethodController")
@RequestMapping("/asl-admin/shipping_method")
public class ShippingMethodController extends BaseController {

    @Resource(name = "shippingMethodServiceImpl")
    private ShippingMethodService shippingMethodService;
    @Resource(name = "deliveryCorpServiceImpl")
    private DeliveryCorpService deliveryCorpService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        model.addAttribute("deliveryCorps", deliveryCorpService.findAll());
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.shippingMethod);
        return "/admin/shipping_method/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(ShippingMethod shippingMethod, Long defaultDeliveryCorpId, RedirectAttributes redirectAttributes) {
        shippingMethod.setDefaultDeliveryCorp(deliveryCorpService.find(defaultDeliveryCorpId));
        if (!isValid(shippingMethod)) {
            return ERROR_VIEW;
        }
        shippingMethod.setPaymentMethods(null);
        shippingMethod.setOrders(null);

        if(shippingMethodService.findDefault()==null){
            shippingMethod.setIsDefault(true);
        }

        if (shippingMethod.getIsDefault()) {
            List<ShippingMethod> sms = shippingMethodService.findAll();
            for (int i = 0; i < sms.size(); i++) {
                if (shippingMethod != sms.get(i)) {
                    sms.get(i).setIsDefault(false);
                    shippingMethodService.update(sms.get(i));
                }
            }
        }
        shippingMethodService.save(shippingMethod);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        model.addAttribute("deliveryCorps", deliveryCorpService.findAll());
        model.addAttribute("shippingMethod", shippingMethodService.find(id));
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.shippingMethod);
        return "/admin/shipping_method/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(ShippingMethod shippingMethod, Long defaultDeliveryCorpId, RedirectAttributes redirectAttributes) {
        shippingMethod.setDefaultDeliveryCorp(deliveryCorpService.find(defaultDeliveryCorpId));
        if (!isValid(shippingMethod)) {
            return ERROR_VIEW;
        }

        if(shippingMethodService.findDefault()==null){
            shippingMethod.setIsDefault(true);
        }

        if(shippingMethod.getIsDefault()==false && shippingMethodService.findDefault().getId()==shippingMethod.getId()){
            addFlashMessage(redirectAttributes, Message.error("请保留一个默认配送方式"));
            return "redirect:list.htm";
        }

        if (shippingMethod.getIsDefault()) {
            List<ShippingMethod> sms = shippingMethodService.findAdminAll();
            for (int i = 0; i < sms.size(); i++) {
                if (shippingMethod != sms.get(i)) {
                    sms.get(i).setIsDefault(false);
                    shippingMethodService.update(sms.get(i));
                }
            }
        }

        shippingMethodService.update(shippingMethod, "paymentMethods", "orders");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        pageable.getFilters().add(Filter.isNull("merchant"));
        model.addAttribute("page", shippingMethodService.findPage(pageable));
        return "/admin/shipping_method/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        if (ids.length >= shippingMethodService.count()) {
            return Message.error("admin.common.deleteAllNotAllowed");
        }

        if (shippingMethodService.count() == ids.length) {
            return Message.error("至少要有一个配送方式");
        }

        if(Arrays.asList(ids).contains(shippingMethodService.findDefault().getId())){
            return Message.error("至少要有一个默认配送方式");
        }
        shippingMethodService.delete(ids);

        return SUCCESS_MESSAGE;
    }

}